package cn.matezk.lianlian.task;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.matezk.lianlian.dao.ApiPushDao;
import cn.matezk.lianlian.model.ApiPuh;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ApiPushTask {
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private ApiPushDao apiPushDao;

//    @PostConstruct
    public void execute() {
        new Thread(() -> {
            while (true) {
                try {
                    // 查询需要推送的批次号 防止重复推送
                    String batchId = IdUtil.fastSimpleUUID();
                    int count = apiPushDao.markBatchId(batchId);
                    if (count > 0) {
                        // 需要推送的
                        List<ApiPuh> records = apiPushDao.selectByBatchId(batchId);
                        if (ObjectUtil.isNotEmpty(records)) {
                            log.info("{}条数据待推送", records.size());
                            for (ApiPuh record : records) {
                                try {
                                    HttpRequest request = new HttpRequest(record.getUrl()).method(Method.POST).body(record.getParams());
                                    record.setPushTime(new Date());
                                    record.setRetryTimes(record.getRetryTimes() + 1);
                                    record.setBatchId("");
                                    HttpResponse response;
                                    try {
                                        response = request.execute();
                                        record.setPushResult(response.getStatus());
                                        response.close();
                                    } catch (Exception e) {
                                        record.setPushResult(0);
                                        log.error(e.getMessage());
                                    } finally {
                                        apiPushDao.updateByPrimaryKeySelective(record);
                                    }
                                } catch (Exception e) {
                                    log.error("数据推送线程异常");
                                    record.setPushResult(0);
                                    apiPushDao.updateByPrimaryKeySelective(record);
                                }
                            }
                        }
                    }
                    Thread.sleep(100);
                } catch (Exception e) {
                    log.error("数据推送异常: {}", e.getMessage());
                }
            }
        }).start();
    }

    @Scheduled(cron = "0 * * * * ?")
    public void clean() {
        apiPushDao.cleanBatchId();
    }
}
